home *** CD-ROM | disk | FTP | other *** search
/ MacPeople 2001 June 15 / MACPEOPLE-2001-06-15.ISO.7z / MACPEOPLE-2001-06-15.ISO / 連載データ / OS X情報局 / x-assist-03j.sit / X-Assist Japanese.dmg / X-Assist プラグイン SDK / MP3 Plugin Example / XAPlugin.m < prev   
Text File  |  2001-04-17  |  7KB  |  291 lines

  1. //
  2. //  Plugin.m
  3. //  MyPlugin
  4. //
  5. //  Created by rosst on Fri Apr 13 2001.
  6. //  Copyright (c) 2001 Ross Tulloch. All rights reserved.
  7. //
  8.  
  9. #import "XAPlugin.h"
  10. #include <Carbon/Carbon.h>
  11.  
  12. // My defines.
  13. #define    kNumberOfDefaultMenuItems    2
  14. #define    kFirstTrackItem            kNumberOfDefaultMenuItems + 1
  15.  
  16. // ============================
  17. // == Change "MP3Plugin" below to your plugin name.
  18. // == Once you build the plugin, place it into the "X-Assist Plugins" folder
  19. // == and everything should just load.
  20. // == NOTE: you MUST use a unique name, otherwise the conflicting plugins
  21. // ==       will NOT load.
  22. // ============================
  23. @implementation MP3Plugin
  24.  
  25. - (id)init
  26. {
  27.     // init plugin internals...
  28.     mPluginMenuItem = NULL;
  29.     
  30.     // ===============================
  31.     // Your Initialization code here...
  32.     // ===============================
  33.     mPlaybackFolder = NULL;
  34.     mCurMovie = NULL;
  35.     mPlaybackItem = 0;
  36.     EnterMovies();
  37.     
  38.     // END Initialization code.
  39.     
  40.     return self;
  41. }
  42.  
  43.  
  44. - (void)shutdownPlugin
  45. {
  46.     // ===============================
  47.     // Your Shutdown code here...
  48.     // ===============================
  49.     [self Cleanup];
  50.  
  51.     if (mPlaybackFolder != NULL)
  52.         [mPlaybackFolder release];
  53.        
  54.     if (mPluginMenuItem != NULL)
  55.         [mPluginMenuItem release];
  56. }
  57.  
  58.  
  59.  
  60. // Required Plugin Routines
  61.  
  62. - (NSMenuItem*)getMenuItem
  63. {
  64.     if (mPluginMenuItem == NULL)
  65.     {
  66.         mPluginMenuItem = [[NSMenuItem alloc] init];
  67.         
  68.         // ===============================
  69.         // Setup our Menu item to our specifications...
  70.         // ===============================
  71.         {
  72.             NSMenuItem*    anItem = NULL;
  73.  
  74.             mSubMenu = [[NSMenu alloc] init];
  75.             [mPluginMenuItem setTitle:@"MP3"];
  76.             
  77.             anItem = [[NSMenuItem alloc] init];
  78.             [anItem setTitle:@"Choose Folderノ"];
  79.             [anItem setEnabled:YES];
  80.             [anItem setAction:@selector(ChooseFolder:) ];
  81.             [anItem setTarget:self];        
  82.             [mSubMenu addItem:anItem];
  83.             [anItem autorelease];
  84.  
  85.             anItem = [[NSMenuItem alloc] init];
  86.             [anItem setTitle:@"Stop Playing"];
  87.             [anItem setEnabled:YES];
  88.             [anItem setAction:@selector(StopPlaying:) ];
  89.             [anItem setTarget:self];        
  90.             [mSubMenu addItem:anItem];
  91.             [anItem autorelease];
  92.             
  93.             [mSubMenu addItem:[NSMenuItem separatorItem]];
  94.  
  95.             [mPluginMenuItem setSubmenu:mSubMenu];
  96.             
  97.             [mSubMenu autorelease];
  98.         }
  99.         // END setup.
  100.         // ===============================
  101.     }
  102.     
  103.     return mPluginMenuItem;
  104. }
  105.  
  106. // items below are for future use.
  107. - (int)getMenuItemInsertLocation
  108. {
  109.     return 0;    // default location.
  110. }
  111.  
  112. - (int)getPluginAPIVersion
  113. {
  114.     return 1;
  115. }
  116.  
  117. // return info & copyright etc.
  118. - (NSString*)getPluginDescription
  119. {
  120.     return @"MP3Plugin v0.1¥r¥rSimple MP3 Player by Ross Tulloch.";
  121. }
  122.  
  123. // put up config window...
  124. - (void)doConfigurePlugin
  125. {
  126.     
  127.     
  128. }
  129.  
  130.  
  131.  
  132.  
  133. // User routines
  134.  
  135. - (IBAction)ChooseFolder:(id)sender
  136. {
  137.     int result;
  138.     NSOpenPanel *oPanel = [NSOpenPanel openPanel];
  139.  
  140.     [[NSApplication sharedApplication] activateIgnoringOtherApps:YES ];
  141.  
  142.     [oPanel setCanChooseDirectories:YES];
  143.     [oPanel setCanChooseFiles:NO];
  144.     
  145.     // Should add this later....
  146.     [oPanel setAllowsMultipleSelection:NO];
  147.     
  148.     result = [oPanel runModalForDirectory:NSHomeDirectory() file:nil types:NULL];
  149.     if (result == NSOKButton)
  150.     {
  151.         NSArray *filesToOpen = [oPanel filenames];
  152.         NSString*        aFile = [filesToOpen objectAtIndex:0];
  153.         NSFileManager*         oFS = [NSFileManager defaultManager];
  154.  
  155.         mPlaybackFolderPath = aFile;
  156.         
  157.         if (mPlaybackFolder != NULL)
  158.             [mPlaybackFolder release];
  159.         mPlaybackFolder = [oFS directoryContentsAtPath:aFile];
  160.         [mPlaybackFolder retain];
  161.         
  162.         // Load file names into menu...
  163.         [self UpdateMenus];
  164.         
  165.         // Start playing....
  166.         [self PlayMenuItemNumber:kFirstTrackItem];
  167.     }
  168. }
  169.  
  170. - (IBAction)StopPlaying:(id)sender
  171. {
  172.     // Stop..
  173.     [self Cleanup];
  174. }
  175.  
  176. - (void)UpdateMenus
  177. {
  178.     // Remove old items...
  179.     {
  180.         int    menuItemCount = [mSubMenu numberOfItems];
  181.         
  182.         while ( --menuItemCount > kNumberOfDefaultMenuItems )
  183.             [mSubMenu removeItemAtIndex:menuItemCount];
  184.     }
  185.     
  186.     // Add new items...
  187.     {
  188.         int i, count = [mPlaybackFolder count];
  189.         for (i=0; i<count; i++) {
  190.                 NSMenuItem* anItem = [[NSMenuItem alloc] init];
  191.                 [anItem setTitle:[mPlaybackFolder objectAtIndex:i]];
  192.                 [anItem setEnabled:YES];
  193.                 [anItem setAction:@selector(PlayMeMenuSelect:) ];
  194.                 [anItem setTarget:self];
  195.                 
  196.                 [mSubMenu addItem:anItem];
  197.                 [anItem autorelease];
  198.         }
  199.     }
  200. }
  201.  
  202. - (IBAction)PlayMeMenuSelect:(id)sender
  203. {
  204.     // Play the item number...
  205.     [self PlayMenuItemNumber:[mSubMenu indexOfItem:sender] ];
  206. }
  207.  
  208. -(void)PlayMenuItemNumber:(int)itemNum
  209. {
  210.     NSMenuItem*    oSelected = [mSubMenu itemAtIndex:itemNum];
  211.  
  212.     // UGLY UGLY UGLY
  213.     char    szString[4096];
  214.     strcpy( szString, [mPlaybackFolderPath cString] );
  215.     strcat( szString, "/" );
  216.     strcat( szString, [[oSelected title] cString] );
  217.     
  218.     // Stop..
  219.     [self Cleanup];
  220.  
  221.     // Start playing...
  222.     [self PlayAMovie:[NSString stringWithCString:szString]];
  223.     
  224.     // Get the number of the item the user selected.
  225.     mPlaybackItem = itemNum;
  226.  
  227.     // Update menu...
  228.     [oSelected setState:NSOnState];
  229. }
  230.  
  231. -(void)PlayAMovie:(NSString*)file
  232. {
  233.     mCurNSMovie = [[NSMovie alloc] initWithURL:[NSURL fileURLWithPath:file] byReference:NO];
  234.     mCurMovie = [mCurNSMovie QTMovie];
  235.     
  236.     StartMovie(mCurMovie);
  237.     
  238.     // Create the timer...
  239.     mPlaybackTimer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector: @selector(TimerProc:) userInfo:NULL repeats:YES];
  240.     
  241. }
  242.  
  243.  
  244. -(void)TimerProc:(NSTimer*)timer
  245. {
  246.     BOOL    bGotoNext = false;
  247.     
  248.     // Has the movie finished.
  249.     if ( mCurMovie != NULL )
  250.         bGotoNext = IsMovieDone(mCurMovie);
  251.     else
  252.         bGotoNext = true;
  253.     
  254.     if ( bGotoNext )
  255.     {
  256.         [self Cleanup];
  257.  
  258.         // Inc the playback item number...
  259.         mPlaybackItem++;
  260.         
  261.         // Should we start again...
  262.         if ( mPlaybackItem >= [mSubMenu numberOfItems] )
  263.             mPlaybackItem = kFirstTrackItem;
  264.  
  265.         // Start playing...
  266.         [self PlayMenuItemNumber:mPlaybackItem];
  267.     }
  268. }
  269.  
  270. -(void)Cleanup
  271. {
  272.     // Remove old tick mark..
  273.     if ( mPlaybackItem )
  274.         [[mSubMenu itemAtIndex:mPlaybackItem]setState:NSOffState];
  275.         
  276.     if ( mCurMovie != NULL ) {
  277.         StopMovie(mCurMovie);
  278.         [mCurNSMovie release];
  279.         mCurMovie = NULL;
  280.     }
  281.  
  282.     // Kill timmer.
  283.     if ( mPlaybackTimer != NULL ) {
  284.         [mPlaybackTimer invalidate];
  285.         mPlaybackTimer = NULL;
  286.     }
  287. }
  288.  
  289.  
  290. @end
  291.